<template>
  <el-dialog :title="formData.id?'编辑客户':'添加客户'" :visible="dialogShow" @close="cancelBtn">
    <el-form ref="clientForm" :model="formData" :rules="rules" label-width="auto">
      <el-form-item label="经纪人" prop="agent">
        <el-select v-model="formData.agent.id" placeholder="请选择经纪人" style="width:90%">
          <el-option v-for="item in agentList" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="formData.email" style="width:90%" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="formData.phone" style="width:90%" />
      </el-form-item>
      <el-form-item label="微信" prop="wechat">
        <el-input v-model="formData.wechat" style="width:90%" />
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <el-input v-model="formData.desc" style="width:90%" type="textarea" :autosize="{ minRows: 3, maxRows: 4}" />
      </el-form-item>
      <el-form-item label="封面" prop="image">
        <UploadImg ref="uploadImg" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelBtn">取 消</el-button>
      <el-button type="primary" @click="confirmBtn">确 定</el-button>
    </div>
  </el-dialog>

</template>

<script>
import { addClient, editClient, getClientById } from '@/api/client'
import UploadImg from '@/views/agents/components/uptupian.vue'
export default {
  components: { UploadImg },
  props: {
    dialogShow: {
      type: Boolean,
      default: false
    },
    agentList: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {
      formData: {
        agent: {
          id: ''
        },
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      },
      rules: {
        agent: [{ required: true, message: '经纪人不能为空', trigger: 'blur' }],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' },
          { min: 2, max: 8, message: '姓名长度为2-8位' }
        ],
        phone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { pattern: /^\w{1,18}@[a-z0-9]+(\.[a-z]{2,4})+$/i, message: '邮箱格式不正确', trigger: 'blur' }],
        wechat: [{ required: true, message: '微信不能为空', trigger: 'blur' }],
        desc: [
          { required: true, message: '描述不能为空', trigger: 'blur' },
          { min: 4, max: 200, message: '描述长度为4-200个字符' }]
      }
    }
  },
  methods: {
    // 根据id获取客户 回填表单
    async getClientById(id) {
      this.formData = await getClientById(id)
      // 客户头像回显
      this.$refs.uploadImg.fileList.push({ url: this.formData.image })
    },
    // 根据this.formData判断是否有id 判断是编辑  还是  添加
    async confirmBtn() {
      if (this.$refs.uploadImg.fileList[0] && !this.$refs.uploadImg.fileList[0].url) {
        return this.$message.error('图片尚未上传完成，请稍后！')
      }
      this.formData.image = this.$refs.uploadImg.fileList[0] && this.$refs.uploadImg.fileList[0].url
      try {
        await this.$refs.clientForm.validate()
        if (this.formData.id) {
          await editClient(this.formData)
          this.$message.success('编辑客户成功！')
        } else {
          await addClient(this.formData)
          this.$message.success('添加客户成功！')
          this.$parent.updateNum()// 添加客户需要重新拉取总数
        }
        this.$parent.getClientList()
        this.cancelBtn()
      } catch (error) {
        console.log(error)
      }
    },
    cancelBtn() {
      this.$refs.clientForm.resetFields()
      this.formData = {
        agent: { id: '' },
        name: '',
        email: '',
        phone: '',
        wechat: '',
        desc: '',
        image: ''
      }
      this.$refs.uploadImg.fileList = []// 重置上传图片组件里面的图片数组
      this.$emit('update:dialogShow', false)
    }
  }

}
</script>

<style>

</style>
